﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ESRI.ArcGIS.AnalysisTools;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.DataSourcesRaster;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.Geoprocessing;

/// <summary>
/// SDEHandle 的摘要说明
/// </summary>
public class SDEHandle
{
	public SDEHandle()
	{
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}
    public SDEHandle(string _server, string _instance, string _user, string _pwd, string _db, string _version)
    {
        this.sdeServer = _server;
        this.sdeInstance = _instance;
        this.sdeUser = _user;
        this.sdePWD = _pwd;
        this.sdeDB = _db;
        this.sdeVersion = _version;
        //InitializeApplication();
    }
    private string sdeServer = "";
    private string sdeInstance = "";
    private string sdeUser = "";
    private string sdePWD = "";
    private string sdeDB = "";
    private string sdeVersion = "";

    private IWorkspaceFactory _wsf;

    public IWorkspaceFactory Wsf
    {
        get { return _wsf; }
        set { _wsf = value; }
    }

    private IRasterWorkspaceEx _rasterWSEx;

    public IRasterWorkspaceEx RasterWSEx
    {
        get { return _rasterWSEx; }
        set { _rasterWSEx = value; }
    }

    //private ESRI.ArcGIS.Geodatabase.IWorkspace _ws;
    //private IRasterDataset _rasterDS;

    #region  初始化许可证
    private IAoInitialize m_AoInitialize = new ESRI.ArcGIS.esriSystem.AoInitializeClass();
    private bool InitializeApplication()
    {
        bool bInitialized = true;
        if(m_AoInitialize==null)
        {
            bInitialized = false;
        }
        //初始化应用程序
        esriLicenseStatus licenseStatus = esriLicenseStatus.esriLicenseUnavailable;

        licenseStatus = CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
        if (licenseStatus != esriLicenseStatus.esriLicenseAlreadyInitialized)
        {
            m_AoInitialize.Shutdown();
            licenseStatus = CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
            if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
            {
                licenseStatus = CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcView);
                if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
                {
                    licenseStatus = CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcEditor);
                    if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
                    {
                        licenseStatus = CheckOutLicenses(esriLicenseProductCode.esriLicenseProductCodeArcInfo);
                        if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)
                        {
                            bInitialized = false;
                        }
                    }
                }
            }
        }
        return bInitialized;
        //m_AoInitialize.Shutdown
    }

    private esriLicenseStatus CheckOutLicenses(esriLicenseProductCode productCode)
    {
        esriLicenseStatus licenseStatus;

        //是否产品是可能的
        licenseStatus = m_AoInitialize.IsProductCodeAvailable(productCode);
        if (licenseStatus == esriLicenseStatus.esriLicenseAvailable)
        {

            //用相应的许可文件进行初始化
            licenseStatus = m_AoInitialize.Initialize(productCode);
        }
        return licenseStatus;
    }

    #endregion

    //3-25
    private void SDEWorkspaceFromPropertySet(String server, String instance, String user, String password, String database, String version)
    {        
        if (InitializeApplication() == true)
        {  
            try
            {
                IPropertySet propertySet = new PropertySetClass();
                propertySet.SetProperty("SERVER", server);
                propertySet.SetProperty("INSTANCE", instance);
                propertySet.SetProperty("DATABASE", database);
                propertySet.SetProperty("USER", user);
                propertySet.SetProperty("PASSWORD", password);
                propertySet.SetProperty("VERSION", version);
                Wsf = (IWorkspaceFactory)new SdeWorkspaceFactoryClass();
                RasterWSEx = (IRasterWorkspaceEx)Wsf.Open(propertySet, 0);
            }
            catch (Exception ex)
            {
                //System.Diagnostics.Debug.WriteLine(ex.Message);
                Log.WriteException(ex.Message);

            }
        }
    }
    public string GetPixel(string filePath, double x, double y)
    {
        string _pixel = "";
        try
        {
            //IPropertySet propertySet = new PropertySetClass();
            //propertySet.SetProperty("SERVER", sdeServer);
            //propertySet.SetProperty("INSTANCE", sdeInstance);
            //propertySet.SetProperty("DATABASE", sdeDB);
            //propertySet.SetProperty("USER", sdeUser);
            //propertySet.SetProperty("PASSWORD", sdePWD);
            //propertySet.SetProperty("VERSION", sdeVersion);
            //IWorkspaceFactory WSF = (IWorkspaceFactory)new SdeWorkspaceFactoryClass();
            //IRasterWorkspaceEx rasterWex = (IRasterWorkspaceEx)WSF.Open(propertySet, 0);            
            //IRasterDataset rasterDs = rasterWex.OpenRasterDataset(filePath);
            SDEWorkspaceFromPropertySet(sdeServer, sdeInstance, sdeUser, sdePWD, sdeDB, sdeVersion);
            IRasterDataset rasterDs = RasterWSEx.OpenRasterDataset(filePath);
            IRaster raster = rasterDs.CreateDefaultRaster();         
            IRaster2 raster2 = (IRaster2)rasterDs.CreateDefaultRaster() as IRaster2;
            int col = raster2.ToPixelColumn(x);
            int row = raster2.ToPixelRow(y);
            _pixel = raster2.GetPixelValue(0, col, row).ToString();            
        }
        catch (Exception ex)
        {
            //System.Diagnostics.Debug.WriteLine(ex.Message);
            Log.WriteException(ex.Message);

        }
        System.Runtime.InteropServices.Marshal.ReleaseComObject(Wsf);//释放SDE工作空间
        m_AoInitialize.Shutdown();
        return _pixel;
    }
}
